/*
 * Copyright (c) 2009, Hamish Morgan. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * 1. Redistributions of source code must retain the above copyright notice,
 *    this list of conditions and the following disclaimer.
 *
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * 3. Neither the name of the University of Sussex nor the names of its
 *    contributors may be used to endorse or promote products derived from
 *    this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */
package locusts.lib.geom;

import java.awt.Polygon;
import java.awt.Rectangle;
import java.awt.Shape;
import java.awt.geom.AffineTransform;
import java.awt.geom.PathIterator;
import java.awt.geom.Point2D;
import java.awt.geom.Rectangle2D;

/**
 *
 * @author Hamish Morgan
 */
public class Triangle implements Shape, Cloneable {

//    private final GeneralPath path;
    private final Polygon poly;
    private final Rectangle bounds;

    /*
     * Scalene
     */
    public Triangle(double x0, double y0, double x1, double y1, double x2,
            double y2) {
        poly = new Polygon();
        poly.addPoint((int) x0, (int) y0);
        poly.addPoint((int) x1, (int) y1);
        poly.addPoint((int) x2, (int) y2);
        bounds = poly.getBounds();
//
//        path = new GeneralPath(GeneralPath.WIND_NON_ZERO, 4);
//        path.moveTo((float) x0, (float) y0);
//        path.lineTo((float) x1, (float) y1);
//        path.lineTo((float) x2, (float) y2);
//        path.closePath();
//        bounds = path.getBounds();
    }

    /*
     * Isosceles
     */
    public Triangle(double x, double y, double w, double h) {
        this(x - w / 2, y - h / 2,
                x + w / 2, y,
                x - w / 2, y + h / 2);
    }

    /*
     * Equilateral
     */
    public Triangle(double x, double y, double length) {
        this(x, y, length, length);
    }

    protected Triangle(Triangle that) {
        this.poly = that.poly;
        this.bounds = that.bounds;
    }

    public Rectangle getBounds() {
        return bounds;
    }

    public Rectangle2D getBounds2D() {
        return bounds;
    }

    public boolean contains(double x, double y) {
        return poly.contains(x, y);
    }

    public boolean contains(Point2D p) {
        return poly.contains(p);
    }

    public boolean intersects(double x, double y, double w, double h) {
        return poly.intersects(x, y, w, h);
    }

    public boolean intersects(Rectangle2D r) {
        return poly.intersects(r);
    }

    public boolean contains(double x, double y, double w, double h) {
        return poly.contains(x, y, w, h);
    }

    public boolean contains(Rectangle2D r) {
        return poly.contains(r);
    }

    public PathIterator getPathIterator(AffineTransform at) {
        return poly.getPathIterator(at);
    }

    public PathIterator getPathIterator(AffineTransform at, double flatness) {
        return poly.getPathIterator(at, flatness);
    }

    @Override
    public Triangle clone() {
        return new Triangle(this);
    }
}
